package xx.services.demo.rest;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Context;

import xx.entity.demo.DemoLayoutItem;
import xx.services.Passport;
import xx.services.demo.inter.ILayoutItemInterface;
import xx.services.util.Constants;
import xx.util.Arr;
import xx.util.string.Format;

/**
 * 布局下的列管理服务接口
 * 
 * @Path (Constants.RESTFUL_ROOT_PATH + "/demo/layout/item")
 * @author Administrator
 * 
 * 2010-12-20
 */
@Path(Constants.RESTFUL_ROOT_PATH + "/demo/layout/item")
public class LayoutItemRest extends Passport implements ILayoutItemInterface
{

    public LayoutItemRest(@Context
    HttpServletRequest request)
    {
        super(request);
        // TODO Auto-generated constructor stub
    }

    /**
     * 删除布局列item demo。<br>
     * 
     * @DELETE
     * @Path ("/{item_id}")
     * @param itemId-@PathParam("item_id")
     *            布局列ID，多个ID用“_”隔开
     * @return true-删除成功，string-其他删除失败（string就是失败原因）
     * @author chenxiang
     */
    @DELETE
    @Path("/{item_id}")
    public String delete(@PathParam("item_id")
    String itemId)
    {
        try
        {
            int count = 0;
            long[] itemIdArr = Arr.toLongArray(itemId, "_");
            for (long id : itemIdArr)
            {
                DemoLayoutItem dli = new DemoLayoutItem(qr, owner);
                dli.setItemId(id);
                dli.delete();
                count++;
            }
            return ok(count);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return error(e);
        }
    }

    /**
     * 获取一个布局列 item demo。<br>
     * 
     * @GET
     * @Path ("/{item_id}")
     * @param itemId-@PathParam("item_id")
     *            布局列ID
     * @return 布局列实体对象
     * @author chenxiang
     */
    @GET
    @Path("/{item_id}")
    public String get(@PathParam("item_id")
    long itemId)
    {
        try
        {
            DemoLayoutItem dli = new DemoLayoutItem(qr, owner);
            dli.setItemId(itemId).load();
            return ok(dli);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return error(e);
        }
    }

    /**
     * 修改一个布局列item demo。<br>
     * 
     * @PUT
     * @Path ("/{item_id}")
     * @param itemId-@PathParam("item_id")
     *            布局列ID
     * @param demo-@FormParam("demo")
     *            布局列DOME实例
     * @param templet-@FormParam("templet")
     *            布局列模板
     * @return 新布局列实体对象
     * @author chenxiang
     */
    @PUT
    @Path("/{item_id}")
    public String update(@PathParam("item_id")
    long itemId, @FormParam("demo")
    String demo, @FormParam("templet")
    String templet)
    {
        try
        {
            DemoLayoutItem dli = new DemoLayoutItem(qr, owner);
            dli.setItemId(itemId);
            StringBuilder condition = new StringBuilder();
            
            if (!Format.isEmpty(demo))
            {
                condition.append("demo,");
                dli.setDemo(demo);
            }
            
            if (!Format.isEmpty(templet))
            {
                condition.append("templet,");
                dli.setTemplet(templet);
            }

            if (condition.length() > 0)
            {
                dli.update(condition.toString());
            }
            return ok(dli);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return error(e);
        }
    }

}
